
# -*- coding:utf-8 -*-
import json
import os

# 获取osma输出
omreport = os.popen('omreport storage pdisk controller=0')
result = omreport.read()
result = result.strip()
# 将结果以空行分割,生成列表,列表的元素为单个磁盘的详情
disks = result.split('\n\n')
# 删除列表第一个元素
disks.pop(0)
# 处理第一个元素的非法格式【添加无用字符】
disks[0] = disks[0].replace(r'Embedded)', r'Embedded): xxxxx')

# 将单个磁盘的详情转换为字典####
disk_pool = []
for disk in disks:
    # 将磁盘详情分割为列表
    item = disk.split('\n')
    disk_dict = {}
    for ele in item:
        key, value = ele.split(': ')
        key = key.strip()
        disk_dict[key] = value
        if key.strip() == 'Serial No.':
            # print(value)
            disk_pool.append({'{#DISK_SN}': value})
print(json.dumps({'data': disk_pool}, indent=4))
